<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      default: function() {
        return {}
      }
    },
    chartType: {
      type: Array
    },
    chartX: {
      type: Array
    },
    stackedArea: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      chart: null,
      typeName: null
    }
  },
  computed: {
    chartDataClone() {
      return this.$utils.deepClone(this.chartData)
    }
  },
  watch: {
    chartDataClone: {
      deep: true,
      handler(val) {
        this.setOptions(this.chartDataClone)
      }
    }
  },
  mounted() {
    this.initChart()
    if (this.autoResize) {
      this.__resizeHanlder = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHanlder)
    }
    // 监听侧边栏的变化
    // const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    // sidebarElm.addEventListener('transitionend', this.__resizeHanlder)

    // 定时检查变化
    setInterval(this.__resizeHanlder, 1000)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHanlder)
    }

    // const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    // sidebarElm.removeEventListener('transitionend', this.__resizeHanlder)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions({ oneData = [], twoData = [], threeData = [], fourData = [] } = {}) {
      this.chart.setOption({
        xAxis: {
          data: this.chartX,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 30,
          right: 60,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          alwaysShowContent: true,
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: this.chartType
        },
        series: [{
          name: this.chartType ? this.chartType[0] : '',
          areaStyle: this.stackedArea ? { normal: {}} : '',
          stack: this.stackedArea ? '总量' : '',
          itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true, // 曲线变折线 false
          type: 'line',
          data: oneData,
          animationDuration: 1000,
          animationEasing: 'cubicInOut'
        },
        {
          name: this.chartType ? this.chartType[1] : '',
          areaStyle: this.stackedArea ? { normal: {}} : '',
          stack: this.stackedArea ? '总量' : '',
          smooth: true, // 曲线变折线 false
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              }
              // areaStyle: {
              //   color: '#f3f8ff'
              // }
            }
          },
          data: twoData,
          animationDuration: 1000,
          animationEasing: 'quadraticOut'
        },
        {
          name: this.chartType ? this.chartType[2] : '',
          areaStyle: this.stackedArea ? { normal: {}} : '',
          stack: this.stackedArea ? '总量' : '',
          smooth: true, // 曲线变折线 false
          type: 'line',
          itemStyle: {
            normal: {
              color: '#facc14',
              lineStyle: {
                color: '#facc14',
                width: 2
              }
              // areaStyle: {
              //   color: '#faf5df'
              // }
            }
          },
          data: threeData,
          animationDuration: 1000,
          animationEasing: 'quadraticOut'
        },
        {
          name: this.chartType ? this.chartType[3] : '',
          smooth: true, // 曲线变折线 false
          type: 'line',
          itemStyle: {
            normal: {
              color: '#223273',
              lineStyle: {
                color: '#223273',
                width: 2
              }
              // areaStyle: {
              //   color: '#d8dbe8'
              // }
            }
          },
          data: fourData,
          animationDuration: 1000,
          animationEasing: 'quadraticOut'
        }
        ]
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.setOptions(this.chartDataClone)
    }
  }
}
</script>
